const chart = {
  state: {
    weatherValue: "",
    typhoonList: [],
    shipList: [],
    otherShipList: []
  },
  actions: {
    setWeatherValue({ commit }, list) {
      commit('SET_WEATHER_VALUE', list)
    },
    setTyphoonList({ commit }, list) {
      commit('SET_TYPHOON_LIST', list)
    },
    refreshShipListByMmsi({ commit }, row) {
      commit('REFRESH_SHIP_LIST_BY_MMSI', row)
    },
    refreshOtherShipListByMmsi({ commit }, row) {
      commit('REFRESH_OTHER_SHIP_LIST_BY_MMSI', row)
    },
  },
  mutations: {
    SET_WEATHER_VALUE: (state, value) => {
      state.weatherValue = value
    },
    SET_TYPHOON_LIST: (state, list) => {
      state.typhoonList = list
    },
    REFRESH_SHIP_LIST_BY_MMSI: (state, row) => {
      const index = state.shipList.findIndex(m => m.mmsi === row.mmsi)
      if (index === -1) {
        state.shipList.push(row)
      } else {
        state.shipList.splice(index, 1, row)
      }
    },
    REFRESH_OTHER_SHIP_LIST_BY_MMSI: (state, row) => {
      const index = state.otherShipList.findIndex(m => m.mmsi === row.mmsi)
      if (index === -1) {
        state.otherShipList.push(row)
      } else {
        state.otherShipList.splice(index, 1, row)
      }
    },
  }
};

export default chart;
